package com.fanx.sample.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.fanx.sample.web.common.JwtUserId;
import com.fanx.sample.web.common.TestConfig;
import com.fanx.sample.web.common.WebResult;
import com.fanx.sample.web.vo.UserVO;

import lombok.extern.slf4j.Slf4j;

/**
 * (User)表服务接口
 * @author makejava
 * @since 2019-09-23 14:27:50
 * @since 2019-09-23 14:48:02
 */
@Slf4j
@RestController
public class UserController implements InitializingBean {
	private List<UserVO> userList = new ArrayList<UserVO>();
	
	@Autowired
	private TestConfig config;
	
    /**
     * @return
     */
	@ResponseBody
	@GetMapping("/users")
    public List<UserVO> getList(@JwtUserId Long userId) {
		System.out.println(config);
		log.info("jwt-userId: {}", userId);
    	List<UserVO> userList = new ArrayList<>();
    	for (int i = 1; i < 10; i++) {
    		UserVO user = new UserVO();
        	user.setId(i+1);
        	user.setName("name-"+i);
        	user.setAge((int) Math.random() * 5 + 5);
        	userList.add(user);
		}
        return userList;
    }
    
    /**
     * @param userVO
     * @return
     */
    @GetMapping("/user/{id}")
    public WebResult<UserVO> getById(@PathVariable("id") Integer id) {
    	for (UserVO user : userList) {
    		if (user.getId().equals(id)) {
    			return WebResult.success(user);
    		}
		}
    	return WebResult.success();
    }
    
    /**
     * @param userVO
     * @return
     */
    @PostMapping("/user/{id}")
    public void update(@PathVariable("id") Integer id) {
    	for (UserVO user : userList) {
    		if (user.getId().equals(id)) {
    			log.info("update user: {}", user);
    			return;
    		}
		}
    	log.info("not found user: {}", id);
    }

	@Override
	public void afterPropertiesSet() throws Exception {
		for (int i = 1; i < 10; i++) {
    		UserVO user = new UserVO();
        	user.setId(i+1);
        	user.setName("name-"+i);
        	user.setAge((int) Math.random() * 5 + 5);
        	userList.add(user);
		}
	}
}